package com.example.common.sendhttp;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.example.common.utils.MapUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author rlj
 * @Date 2021/4/21 16:53
 * @Version 1.0
 */

@Component
public class HxSendHttp {

    public JSONObject getToken(String grantType, String clientId, String clientSecret) {
        Map<String, String> map = new HashMap<>(MapUtils.getMapInitSize(3));
        map.put("grant_type", grantType);
        map.put("client_id", clientId);
        map.put("client_secret", clientSecret);
        String post = HttpUtil.post("https://a1.easemob.com/1103190516010105/hlwyy/token", JSONObject.toJSONString(map));
        return JSON.parseObject(post);
    }


    /**
     * 获得视频的下载url
     * @param token 请求的token
     * @param orderId 订单的id
     * @return 返回下载视频url 的集合
     */
    public List<String> getVideoList(String token, String orderId){
        List<String> urlList = new ArrayList<>();
        String result = HttpRequest.get("https://a1.easemob.com/1103190516010105/hlwyy/audio/" + orderId)
                .header("Authorization",    token).execute().body();
        List list = JSON.parseObject(JSON.parseObject(result).get("data").toString(), List.class);

        // 取出url保存在集合中
        list.forEach(t ->{
            urlList.add(JSONObject.parseObject(t.toString()).get("url").toString());
        });
        return urlList;
    }

}
